<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分" prop="rating">
        <el-select v-model="queryParams.rating" placeholder="请选择评分" clearable>
          <el-option
            v-for="dict in ratingOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['comment:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['comment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论ID" align="center" prop="commentId" width="80" />
      <el-table-column label="用户名" align="center" prop="username" :show-overflow-tooltip="true" />
      <el-table-column label="订单号" align="center" prop="orderId" width="100" />
      <el-table-column label="评分" align="center" prop="rating" width="80">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.rating"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['comment:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReply(scope.row)"
            v-hasPermi="['comment:reply']"
          >回复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['comment:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看评论详情对话框 -->
    <el-dialog title="评论详情" :visible.sync="viewDialogVisible" width="600px" append-to-body>
      <el-descriptions class="comment-detail" :column="1" border>
        <el-descriptions-item label="评论ID">{{ viewForm.commentId }}</el-descriptions-item>
        <el-descriptions-item label="用户名">{{ viewForm.username }}</el-descriptions-item>
        <el-descriptions-item label="订单号">{{ viewForm.orderId }}</el-descriptions-item>
        <el-descriptions-item label="评分">
          <el-rate
            v-model="viewForm.rating"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-descriptions-item>
        <el-descriptions-item label="评论内容">
          <div class="comment-content">{{ viewForm.content }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(viewForm.createTime) }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 回复评论对话框 -->
    <el-dialog title="回复评论" :visible.sync="replyDialogVisible" width="600px" append-to-body>
      <el-form ref="replyForm" :model="replyForm" :rules="replyRules" label-width="80px">
        <el-form-item label="评论内容">
          <div class="original-comment">
            <div class="comment-user">{{ replyForm.username }} <el-rate v-model="replyForm.rating" disabled></el-rate></div>
            <div class="comment-text">{{ replyForm.content }}</div>
            <div class="comment-time">{{ parseTime(replyForm.createTime) }}</div>
          </div>
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent">
          <el-input
            v-model="replyForm.replyContent"
            type="textarea"
            placeholder="请输入回复内容"
            :rows="4"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReply">确 定</el-button>
        <el-button @click="replyDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComments, getComment, deleteComment, replyComment } from "@/api/company/comment";

export default {
  name: "CommentManage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论表格数据
      commentList: [],
      // 日期范围
      dateRange: [],
      // 评分选项
      ratingOptions: [
        { value: 1, label: "1星" },
        { value: 2, label: "2星" },
        { value: 3, label: "3星" },
        { value: 4, label: "4星" },
        { value: 5, label: "5星" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        orderId: null,
        rating: null
      },
      // 查看表单
      viewForm: {},
      // 查看对话框可见性
      viewDialogVisible: false,
      // 回复表单
      replyForm: {},
      // 回复对话框可见性
      replyDialogVisible: false,
      // 回复表单校验规则
      replyRules: {
        replyContent: [
          { required: true, message: "回复内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评论列表 */
    getList() {
      this.loading = true;
      listComments(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commentId);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 查看详情操作 */
    handleView(row) {
      getComment(row.commentId).then(response => {
        this.viewForm = response.data;
        this.viewDialogVisible = true;
      });
    },
    /** 回复操作 */
    handleReply(row) {
      this.replyForm = { ...row, replyContent: '' };
      this.replyDialogVisible = true;
    },
    /** 提交回复 */
    submitReply() {
      this.$refs["replyForm"].validate(valid => {
        if (valid) {
          const data = {
            commentId: this.replyForm.commentId,
            replyContent: this.replyForm.replyContent
          };
          
          replyComment(data).then(response => {
            this.$modal.msgSuccess("回复成功");
            this.replyDialogVisible = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal.confirm('是否确认删除评论编号为"' + commentIds + '"的数据项？').then(function() {
        return deleteComment(commentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('comment/export', {
        ...this.queryParams
      }, `评论数据_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>

<style scoped>
.comment-content {
  white-space: pre-wrap;
  word-break: break-all;
  min-height: 60px;
  max-height: 150px;
  overflow-y: auto;
  padding: 10px;
  background-color: #f8f8f8;
  border-radius: 4px;
}

.original-comment {
  background-color: #f8f8f8;
  border-radius: 4px;
  padding: 15px;
  margin-bottom: 15px;
}

.comment-user {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-weight: bold;
}

.comment-text {
  margin-bottom: 10px;
  white-space: pre-wrap;
  word-break: break-all;
}

.comment-time {
  font-size: 12px;
  color: #909399;
  text-align: right;
}
</style> 